package com.kevin.map.direction;

import com.google.maps.*;
import com.google.maps.model.DirectionsRoute;
import com.google.maps.model.GeocodingResult;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2015/10/17.
 */
public class DirectionServlet extends javax.servlet.http.HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        System.out.println(request);
        PrintWriter writer = response.getWriter();

        String str = "";
        GeoApiContext context = new GeoApiContext().setApiKey("AIzaSyCFlgM9tumtC2R0yj6di__KJxDP4M6UMZ0");
        context.setQueryRateLimit(2500)
                .setConnectTimeout(60, TimeUnit.SECONDS)
                .setReadTimeout(60, TimeUnit.SECONDS)
                .setWriteTimeout(60, TimeUnit.SECONDS);

        try {
            System.out.println("DirectionsApi.getDirections");
            DirectionsApiRequest apiRequest = DirectionsApi.getDirections(context, "Toronto",
                    "Montreal");
//
//            str = apiRequest.toString();
//            System.out.println("setCallback");
//            apiRequest.setCallback(new PendingResult.Callback<DirectionsRoute[]>() {
//                public void onResult(DirectionsRoute[] routes) {
//                    System.out.println("onResult routes: " + routes);
//                    System.out.println(routes[0]);
//
//                }
//
//                public void onFailure(Throwable e) {
//                    System.out.println("onFailure " + e.getMessage());
//
//                    try {
//                        response.getWriter().write(e.getMessage());
//                    } catch (IOException e1) {
//                        e1.printStackTrace();
//                    }
//                }
//            });
//
            System.out.println("DirectionsApi req");
            writer.write("DirectionsApi req");
            DirectionsRoute[] routes = apiRequest.await();

            System.out.println("DirectionsApi req ok" +routes);
            writer.write("DirectionsApi req ok" +routes);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("DirectionsApi req error " +e.getMessage());
            writer.write("DirectionsApi req error " +e.getMessage());
        }



//        GeocodingApiRequest req = GeocodingApi.newRequest(context).address("Sydney");
//        try {
//            System.out.println("GeocodingApi req");
//            writer.write("GeocodingApi req");
//            GeocodingResult[] results = req.await();
//            System.out.println("GeocodingApi req ok" +results);
//            writer.write("GeocodingApi req ok" +results);
//            // Handle successful request.
//        } catch (Exception e) {
//            // Handle error
//            System.out.println("GeocodingApi req error " +e.getMessage());
//            writer.write("GeocodingApi req error " +e.getMessage());
//        }
    }
}
